package com.smbpics

import android.content.Context
import android.content.SharedPreferences

data class SmbConnectionConfig(
    val serverAddress: String = "",
    val shareName: String = "",
    val username: String = "",
    val password: String = ""
)

class ConnectionSettings(private val context: Context) {
    companion object {
        private const val PREFS_NAME = "smb_connection_prefs"
        private const val KEY_SERVER_ADDRESS = "server_address"
        private const val KEY_SHARE_NAME = "share_name"
        private const val KEY_USERNAME = "username"
        private const val KEY_PASSWORD = "password"
        private const val KEY_AUTO_CONNECT = "auto_connect"
        private const val KEY_LAST_CONNECTION_TIME = "last_connection_time"
        private const val KEY_CONNECTION_SUCCESS_COUNT = "connection_success_count"
    }

    private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun saveConnection(config: SmbConnectionConfig) {
        prefs.edit()
            .putString(KEY_SERVER_ADDRESS, config.serverAddress)
            .putString(KEY_SHARE_NAME, config.shareName)
            .putString(KEY_USERNAME, config.username)
            .putString(KEY_PASSWORD, config.password)
            .putLong(KEY_LAST_CONNECTION_TIME, System.currentTimeMillis())
            .apply()
    }

    fun getLastConnection(): SmbConnectionConfig {
        return SmbConnectionConfig(
            serverAddress = prefs.getString(KEY_SERVER_ADDRESS, "") ?: "",
            shareName = prefs.getString(KEY_SHARE_NAME, "") ?: "",
            username = prefs.getString(KEY_USERNAME, "") ?: "",
            password = prefs.getString(KEY_PASSWORD, "") ?: ""
        )
    }

    fun clearConnection() {
        prefs.edit()
            .remove(KEY_SERVER_ADDRESS)
            .remove(KEY_SHARE_NAME)
            .remove(KEY_USERNAME)
            .remove(KEY_PASSWORD)
            .remove(KEY_LAST_CONNECTION_TIME)
            .remove(KEY_CONNECTION_SUCCESS_COUNT)
            .apply()
    }
    
    fun setAutoConnect(enabled: Boolean) {
        prefs.edit().putBoolean(KEY_AUTO_CONNECT, enabled).apply()
    }
    
    fun isAutoConnectEnabled(): Boolean {
        return prefs.getBoolean(KEY_AUTO_CONNECT, true)
    }
    
    fun recordConnectionSuccess() {
        val currentCount = prefs.getInt(KEY_CONNECTION_SUCCESS_COUNT, 0)
        prefs.edit()
            .putInt(KEY_CONNECTION_SUCCESS_COUNT, currentCount + 1)
            .putLong(KEY_LAST_CONNECTION_TIME, System.currentTimeMillis())
            .apply()
    }
    
    fun getConnectionStats(): ConnectionStats {
        return ConnectionStats(
            successCount = prefs.getInt(KEY_CONNECTION_SUCCESS_COUNT, 0),
            lastConnectionTime = prefs.getLong(KEY_LAST_CONNECTION_TIME, 0)
        )
    }
    
    fun hasSavedConnection(): Boolean {
        val lastConnection = getLastConnection()
        return lastConnection.serverAddress.isNotEmpty() && lastConnection.shareName.isNotEmpty()
    }
}

data class ConnectionStats(
    val successCount: Int,
    val lastConnectionTime: Long
)